package com.isoftstone.demo.tree.controller;

import java.util.List;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.isoftstone.demo.tree.entity.TreeNodeData;
import com.isoftstone.demo.tree.service.DemoTreeService;
import com.isoftstone.services.base.RespBaseController;
import com.isoftstone.services.base.vo.TreeNode;

/**
 * <br> Demo树的Controller
 * @author  tulinli
 * @version  [版本号, 2017年11月24日]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
@RestController
@RequestMapping({"/demo"})
public class DemoTreeController extends RespBaseController<DemoTreeService>
{
    @Override
    public Long getModuleId()
    {
        
        return null;
    }
    
    /**
     * 
     * <br> 加载树的数据
     * @param lang
     * @param parentId
     * @param level
     * @return
     * @author  tulinli
     * @see [类、类#方法、类#成员]
     */
    @RequestMapping("/asyncTree")
    public String tree(@RequestParam(value = "lang") String lang,
        @RequestParam(value = "id", required = false) Integer parentId,
        @RequestParam(value = "level", required = false) Integer level)
    {
        List<TreeNode> tree = service.tree(lang, parentId, level);
        return toJson(tree);
    }
    
    /**
     * 
     * <br> 新增树节点
     * @param node
     * @return
     * @throws Exception
     * @author  tulinli
     * @see [类、类#方法、类#成员]
     */
    @PostMapping
    public String add(TreeNodeData node)
        throws Exception
    {
        // 操作对象（用于生成日志）
        Operation operation =
            new Operation("log.treeNode.add").setMsgArgs(node.getName());
        // 执行业务、生成日志、返回Json
        return respJson(() -> toJson(service().add(node)), operation);
    }
    
    /**
    * 
    * <br> 修改树节点
    * @param node
    * @return
    * @throws Exception
    * @author  tulinli
    * @see [类、类#方法、类#成员]
    */
    @PutMapping
    public String update(@RequestBody TreeNodeData node)
        throws Exception
    {
        // 操作对象（用于生成日志）
        Operation operation =
            new Operation("log.treeNode.update").setMsgArgs(node.getName());
        return respSuccess(() -> service.save(node), operation);
    }
    
    /**
     * 删除节点
     */
    @DeleteMapping(value = "/{id}")
    public String delete(@PathVariable Integer id)
        throws Exception
    {
        // 操作对象（用于生成日志）
        Operation operation = new Operation("log.treeNode.delete");
        return respSuccess(() -> {
            service.delete(id);
        }, operation);
    }
    
}
